<template>
  <div class="container">
    <el-card>
      <el-form label-width="80px" ref="questionRef" :model="questionSize">
        <el-form-item label="关键字" prop="keyword">
          <el-input
            v-model="questionSize.keyword"
            placeholder="根据编号搜索"
            style="width: 300px; margin-right: 700px;"
          ></el-input>
          <el-button size="small" @click="clearQuestion">清除</el-button>
          <el-button size="small" type="primary" @click="searchData">
            搜索
          </el-button>
        </el-form-item>
      </el-form>
      <!-- 消息弹框 -->
      <el-alert
        :title="text"
        type="info"
        show-icon
        :closable="false"
        class="alertBox"
      ></el-alert>
      <el-table style="width: 100%;" :data="questionData">
        <el-table-column label="编号" width="190">
          <template slot-scope="scope">
            <span>{{ scope.row.id }}</span>
          </template>
        </el-table-column>
        <el-table-column label="题型" width="90">
          <template slot-scope="scope">
            <span v-if="scope.row.questionType == 1">单选</span>
            <span v-else-if="scope.row.questionType == 2">多选</span>
            <span v-else>简答</span>
          </template>
        </el-table-column>
        <el-table-column label="题目编号" width="210">
          <template slot-scope="scope">
            <li
              v-for="(item, index) in scope.row.questionIDs"
              :key="index"
              @click="itemIdClick"
            >
              {{ item.number }}
            </li>
          </template>
        </el-table-column>
        <el-table-column label="录入时间">
          <template slot-scope="scope">
            <span>{{ scope.row.addTime | parseTimeByString }}</span>
          </template>
        </el-table-column>
        <el-table-column label="答题时间(s)">
          <template slot-scope="scope">
            <span>{{ scope.row.totalSeconds }}</span>
          </template>
        </el-table-column>
        <el-table-column label="正确率(%)">
          <template slot-scope="scope">
            <span>{{ scope.row.accuracyRate }}</span>
          </template>
        </el-table-column>
        <el-table-column label="录入人">
          <template slot-scope="scope">
            <span>{{ scope.row.userName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="danger"
              icon="el-icon-delete"
              plain
              circle
              @click="deleteData(scope.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="questionSize.pageSize"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="Number(questionSize.pageSize)"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        ></el-pagination>
      </div>
    </el-card>
    <!--    弹出的对话框   -->

    <el-dialog title="提示" :visible.sync="randomsdialogVisible" width="50%">
      <el-row>
        <el-col :span="6">【题型】：</el-col>
        <el-col :span="6">【编号】：</el-col>
        <el-col :span="6">【难度】：</el-col>
        <el-col :span="6">【标签】：</el-col>
        <el-col :span="6">【学科】：</el-col>
        <el-col :span="6">【目录】：</el-col>
        <el-col :span="6">【方向】：</el-col>
      </el-row>
      <hr />
      <el-row>
        <el-col>
          【题干】：
          <br />
          选项：（以下选中的选项为正确答案）
        </el-col>
      </el-row>
      <hr />
      <el-row>
        <el-col>
          【参考答案】：
          <el-button type="danger">视频答案预览</el-button>
        </el-col>
      </el-row>
      <el-row>
        <el-col>【答案解析】：</el-col>
      </el-row>
      <hr />
      <el-row>
        <el-col>
          【题目备注】：
        </el-col>
      </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button @click="randomsdialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="randomsdialogVisible = false">
          确 定
        </el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { questionType } from '../../api/hmmm/constants'
import { randoms, removeRandoms } from '../../api/hmmm/questions'
import { parseTimeByString } from '../../filters/index'

export default {
  data() {
    return {
      questionData: [],
      text: '',
      total: 0,
      randomsdialogVisible: false,
      questionSize: {
        page: 1,
        pageSize: 10,
        keyword: null,
      },
    }
  },
  methods: {
    async getQuestionsData() {
      const { data } = await randoms({
        page: this.questionSize.page,
        pagesize: this.questionSize.pageSize,
        keyword: this.questionSize.keyword,
      })
      console.log(data)
      this.questionData = data.items
      // console.log(this.questionData)
      this.total = data.counts
      this.text = `数据一共 ${this.total} 条`
    },
    handleSizeChange(val) {
      this.questionSize.pageSize = val
      this.getQuestionsData()
    },
    handleCurrentChange(val) {
      this.questionSize.page = val
      this.getQuestionsData()
    },
    // 清除表单数据
    clearQuestion() {
      this.$refs.questionRef.resetFields()
    },
    // 搜索
    searchData() {
      this.getQuestionsData({
        keyword: this.questionSize.keyword,
      })
    },
    // 删除
    async deleteData(scope) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(async () => {
          await removeRandoms({
            id: scope.id,
          })
          this.$message({
            type: 'success',
            message: '删除成功!',
          })
          this.getQuestionsData()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除',
          })
        })
    },
    // 点击出现的弹框
    itemIdClick() {
      console.log(123)
      this.randomsdialogVisible = true
    },
  },
  created() {
    this.getQuestionsData()
  },
}
</script>

<style scoped lang="scss">
li {
  list-style: none;
  color: #0099ff;
}
li:hover {
  cursor: pointer;
}
.alertBox {
  margin-bottom: 15px;
}
.pagination {
  float: right;
  margin: 15px 5px;
}
.el-col {
  padding: 10px 0;
}
</style>
<style scoped></style>
